﻿using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(AnimationController))]
public class Controller : MonoBehaviour
{
    /// <summary>
    /// 滑动方向
    /// </summary>
    public enum DirectionInput { None, Left, Right, Up, Down, }
    /// <summary>
    /// 在道路的中间，左边， 右边
    /// </summary>
    public enum Road { Middle, Left, Right,}

    public static Controller Instance;
    [HideInInspector]
    public CharacterController CharacterController;
    private Animation Animation;
    public float MoveSpeed;
    public float JumpValue;//上跳的高度
    public float Gravity;//重力

    public bool MouseInput;//鼠标输入
    public bool FingerInput;//手指输入
    [HideInInspector]
    public bool IsDoubleJump;//两连跳
    [HideInInspector]
    public bool IsRoll;//滚动

    private bool ActiveInput;//激活滑动输入
    private bool IsJumpSecond;

    private DirectionInput DirectInput;
    private Road RoadOnStand;

    private AnimationController AnimationController;

    private Vector2 CurrentPos;//当前鼠标位置
    private Vector3 MoveDir;//移动方向

    private void Awake()
    {
        Instance = this;
        this.CharacterController = GetComponent<CharacterController>();
        this.Animation = GetComponent<Animation>();
        this.AnimationController = GetComponent<AnimationController>();
        this.MoveSpeed = GameAttribute.Instance.Speed;
        this.IsJumpSecond = false;
        Invoke("OnWaitStart", 0.2f);
    }

    /// <summary>
    /// 等待开始奔跑
    /// </summary>
    private void OnWaitStart()
    {
        StartCoroutine(OnUpdateAction());
    }

    private IEnumerator OnUpdateAction()
    {
        while (GameAttribute.Instance.Life > 0)
        {
            if (!GameAttribute.Instance.Pause && GameAttribute.Instance.IsPlaying)
            {
                if (MouseInput)
                    KeyInput();
                if (FingerInput)
                    DirectionAngleInput();
                CheckLane();
                MoveForward();
            }
            else
                this.Animation.Stop();
            yield return 0;
        }
        StartCoroutine(OnMoveBack());
        this.AnimationController.AnimationState = AnimationController.OnDead;
    }

    /// <summary>
    /// 后移一小段距离
    /// </summary>
    /// <returns></returns>
    private IEnumerator OnMoveBack()
    {
        float z = transform.position.z - 0.5f;
        bool complete = false;
        while (!complete)
        {
            transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, transform.position.y, z), 2 * Time.deltaTime);
            if ((transform.position.x - z) < 0.05f)
                complete = true;
            yield return 0;
        }
        yield return 0;
    }

    private void KeyInput()
    {
        if (Input.anyKeyDown)
            ActiveInput = true;
        if (ActiveInput)
        {
            if (Input.GetKey(KeyCode.A))
                DirectInput = DirectionInput.Left;
            else if (Input.GetKey(KeyCode.D))
                DirectInput = DirectionInput.Right;
            else if (Input.GetKey(KeyCode.W))
                DirectInput = DirectionInput.Up;
            else if (Input.GetKey(KeyCode.S))
                DirectInput = DirectionInput.Down;
            ActiveInput = false;
        }
        else
            DirectInput = DirectionInput.None;
    }

    /// <summary>
    /// 走在哪条路 右边，中间，左边
    /// </summary>
    private void CheckLane()
    {
        if (RoadOnStand == Road.Middle)
        {
            if (DirectInput == DirectionInput.Right)
            {
                if (CharacterController.isGrounded)
                {
                    this.Animation.Stop();
                    AnimationController.AnimationState = AnimationController.OnTurnRight;
                }
                RoadOnStand = Road.Right;
            }
            else if (DirectInput == DirectionInput.Left)
            {
                if (CharacterController.isGrounded)
                {
                    this.Animation.Stop();
                    AnimationController.AnimationState = AnimationController.OnTurnLeft;
                }
                RoadOnStand = Road.Left;
            }
            transform.position = Vector3.Lerp(transform.position, new Vector3(0f, transform.position.y, transform.position.z), 6 * Time.deltaTime);
        }
        else if (RoadOnStand == Road.Left)
        {
            if (DirectInput == DirectionInput.Right)
            {
                if (CharacterController.isGrounded)
                {
                    this.Animation.Stop();
                    AnimationController.AnimationState = AnimationController.OnTurnRight;
                }
                RoadOnStand = Road.Middle;
            }
            transform.position = Vector3.Lerp(transform.position, new Vector3(-1.8f, transform.position.y, transform.position.z), 6 * Time.deltaTime);
        }
        else if (RoadOnStand == Road.Right)
        {
            if (DirectInput == DirectionInput.Left)
            {
                if (CharacterController.isGrounded)
                {
                    this.Animation.Stop();
                    AnimationController.AnimationState = AnimationController.OnTurnLeft;
                }
                RoadOnStand = Road.Middle;
            }
            transform.position = Vector3.Lerp(transform.position, new Vector3(1.8f, transform.position.y, transform.position.z), 6 * Time.deltaTime);
        }

        if (DirectInput == DirectionInput.Down)
        {
            this.Animation.Stop();
            AnimationController.AnimationState = AnimationController.OnRoll;
        }
    }

    /// <summary>
    /// 向前移动
    /// </summary>
    private void MoveForward()
    {
        MoveSpeed = GameAttribute.Instance.Speed;
        if (CharacterController.isGrounded)
        {
            MoveDir = Vector3.zero;
            if (DirectInput == DirectionInput.Up)
            {
                Jump();
                if (IsDoubleJump)//要两连跳
                    IsJumpSecond = true;//触发第二次跳
            }
        }
        else
        {
            if (DirectInput == DirectionInput.Down)
                QuickGround();
            if (DirectInput == DirectionInput.Up)
            {
                if (IsJumpSecond)
                {
                    JumpSecond();
                    IsJumpSecond = false;
                }
            }

            if (AnimationController.AnimationState != AnimationController.OnJump &&
                AnimationController.AnimationState != AnimationController.OnJumpSecond &&
                AnimationController.AnimationState != AnimationController.OnRoll)
            {
                AnimationController.AnimationState = AnimationController.OnJumpLoop;
            }
        }

        MoveDir.z = 0f;
        MoveDir += this.transform.TransformDirection(Vector3.forward * MoveSpeed);
        MoveDir.y -= Gravity * Time.deltaTime;

        CharacterController.Move(MoveDir * Time.deltaTime);
    }

    /// <summary>
    /// 跳起
    /// </summary>
    private void Jump()
    {
        AnimationController.AnimationState = AnimationController.OnJump;
        MoveDir.y += JumpValue;
    }

    /// <summary>
    /// 快速落到地面
    /// </summary>
    private void QuickGround()
    {
        MoveDir.y -= JumpValue * 3;
    }

    /// <summary>
    /// 第二次跳
    /// </summary>
    private void JumpSecond()
    {
        AnimationController.AnimationState = AnimationController.OnJumpSecond;
        MoveDir.y += JumpValue * 1.15f;
    }

    private void TouchInput()
    {
        if (Input.GetMouseButtonDown(0))
        {
            CurrentPos = Input.mousePosition;
            ActiveInput = true;
        }

        if (Input.GetMouseButton(0))
        {
            if (ActiveInput)
            {
                if ((Input.mousePosition.x - CurrentPos.x) > 40f)
                {
                    DirectInput = DirectionInput.Right;
                    ActiveInput = false;
                }
                else if ((Input.mousePosition.x - CurrentPos.x) < -40f)
                {
                    DirectInput = DirectionInput.Left;
                    ActiveInput = false;
                }
                else if ((Input.mousePosition.y - CurrentPos.y) > 40f)
                {
                    DirectInput = DirectionInput.Up;
                    ActiveInput = false;
                }
                else if ((Input.mousePosition.y - CurrentPos.y) < -40f)
                {
                    DirectInput = DirectionInput.Down;
                    ActiveInput = false;
                }
            }
            else
                DirectInput = DirectionInput.None;
        }

        if (Input.GetMouseButtonUp(0))
            DirectInput = DirectionInput.None;
        CurrentPos = Input.mousePosition;
    }

    /// <summary>
    /// 手指滑动
    /// </summary>
    private void DirectionAngleInput()
    {
        if (Input.GetMouseButtonDown(0))
        {
            CurrentPos = Input.mousePosition;
            ActiveInput = true;
        }

        if (Input.GetMouseButton(0))
        {
            if (ActiveInput)
            {
                float ang = GameUtilities.GetAngle(CurrentPos, Input.mousePosition);
                //Debug.Log(ang);
                if ((Input.mousePosition.x - CurrentPos.x) > 20f)
                {
                    if (ang < 45f && ang > -45f)
                        DirectInput = DirectionInput.Right;
                    else if (ang >= 45f)
                        DirectInput = DirectionInput.Up;
                    else if (ang <= -45f)
                        DirectInput = DirectionInput.Down;
                }
                else if ((Input.mousePosition.x - CurrentPos.x) < -20f)
                {
                    if (ang < 45f && ang > -45f)
                        DirectInput = DirectionInput.Left;
                    else if (ang >= 45f)
                        DirectInput = DirectionInput.Down;
                    else if (ang <= -45f)
                        DirectInput = DirectionInput.Up;
                }
                else if ((Input.mousePosition.y - CurrentPos.y) > 20f)
                {
                    if ((Input.mousePosition.x - CurrentPos.x) > 0)
                    {
                        if (ang > 45f && ang <= 90f)
                            DirectInput = DirectionInput.Up;
                        else if (ang >= -45f && ang <= 45f)
                            DirectInput = DirectionInput.Right;
                    }
                    else if ((Input.mousePosition.x - CurrentPos.x) < 0)
                    {
                        if (ang < -45f && ang >= -89f)
                            DirectInput = DirectionInput.Up;
                        else if (ang >= -45f)
                            DirectInput = DirectionInput.Left;
                    }
                }
                else if ((Input.mousePosition.y - CurrentPos.y) < -20f)
                {
                    if ((Input.mousePosition.x - CurrentPos.x) > 0f)
                    {
                        if (ang > -89f && ang < -45f)
                            DirectInput = DirectionInput.Down;
                        else if (ang >= -45f)
                            DirectInput = DirectionInput.Right;
                    }
                    else if ((Input.mousePosition.x - CurrentPos.x) < 0)
                    {
                        if (ang > 45f && ang < 89f)
                            DirectInput = DirectionInput.Down;
                        else if (ang <= 45f)
                            DirectInput = DirectionInput.Left;
                    }
                }
                ActiveInput = false;
            }
        }
    }
}
